package com.example.pkmedical.Activity.warning;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class LeaveBedStartingActivity extends BaseActivity {


    @BindView(R.id.leaveing_bed_img)
    ImageView leaveingBedImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leavebedstarting);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        TimeCount();
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.GET_WEIGHT_LEAVE);
    }
    @Override
    public void initView() {
        switch (CommandUtil.LEAVEBED_TYPE) {
            case 1:
                leaveingBedImg.setImageResource(R.drawable.img_staring_right);
                break;
            case 2:
                leaveingBedImg.setImageResource(R.drawable.img_leaveing);
                break;
            case 3:
                leaveingBedImg.setImageResource(R.drawable.img_leaved);
                break;
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    /**
     * 倒计时
     */
    private Disposable mTimeCountDisposable;

    public void TimeCount() {
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposable = Flowable.intervalRange(0, 2, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {

                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposable != null) {
                        mTimeCountDisposable.dispose();
                    }
                    if (CommandUtil.LEAVEBED_TYPE != 0) {
                        jumpOk();
                    } else {
                        LeaveBedStartingActivity.this.finish();
                    }
                })
                .subscribe();
    }

    private void jumpOk() {
        Intent intent = new Intent();
        intent.putExtra("warning_type", 1);
        intent.setClass(MyApp.getAppContext(), LeaveBedOkActivity.class);
        startActivity(intent);
        LeaveBedStartingActivity.this.finish();
    }
}
